import { _decorator, Component, director, instantiate, Node, Prefab, UITransform } from 'cc';
import { CircleManager } from './CircleManager';
import { RendorManager } from '../Base/RendorManager';
import DataManger from '../RunTime/DataManager';
import { SceneEnum, TriggerStatusEnum } from '../Enum';

const { ccclass, property } = _decorator;

@ccclass('H2AGameManager')
export class H2AGameManager extends RendorManager {
    @property([CircleManager])
    circles: CircleManager[] = []

    @property(Prefab)
    line: Prefab = null
    
    @property(Node)
    lines: Node = null

    @property([Prefab])
    contentPF: Prefab[] = []


    circlesMap: Map<CircleManager, CircleManager[]> = new Map()
    
    protected start(): void {
        super.start()
        this.generateMap()
        this.generateLines()
        this.checkSuccess()


    }
    
    /** 渲染 */
    rendor(): void {
        for (let i = 0; i < this.circles.length; i++){
            const circle = this.circles[i]
            circle.node.destroyAllChildren()

            const contentIndex = DataManger.Instance.H2AData[i]
            if (contentIndex !== null && this.contentPF[contentIndex]) {
                const content = instantiate(this.contentPF[contentIndex])
                circle.node.addChild(content)
            }
        }
    }

    /** 节点点击处理 */
    handleCiricleTouch(e: Event, _index: string) {
        if (DataManger.Instance.DoorStatus === TriggerStatusEnum.Resolved) {
            return
        }
        const index = parseInt(_index)
        const touchIndex = DataManger.Instance.H2AData[index]
        if (touchIndex === null) {
            return
        }
        const curCircle = this.circles[index]
        const circles = this.circlesMap.get(curCircle)
        
        for (let i = 0; i < circles.length; i++){
            const circle = circles[i]
            const nullIndex = DataManger.Instance.H2AData.findIndex(i => i === null)
            const circleIndex = this.circles.findIndex(i => i === circle)
            if (circleIndex === nullIndex) {
                DataManger.Instance.H2AData[circle.index] = touchIndex
                DataManger.Instance.H2AData[index] = null
                DataManger.Instance.H2AData=[...DataManger.Instance.H2AData]
            }
        }

        this.checkSuccess()
        
    }
    
    checkSuccess() {
        if (DataManger.Instance.H2AData.every((e, i) => DataManger.Instance.H2AAnswer[i] === e)) {
            DataManger.Instance.DoorStatus = TriggerStatusEnum.Resolved
            setTimeout(() => {
                DataManger.Instance.curScene=SceneEnum.H2
            },300)
        }
    }

    /** 重置game */
    resetGame() {
        DataManger.Instance.H2AData= [...DataManger.Instance.H2AInitData]
    }

    /** 初始化map数据 */
    generateMap() {
        this.circlesMap.set(this.circles[0], [this.circles[1],this.circles[4],this.circles[6]])
        this.circlesMap.set(this.circles[1], [this.circles[0],this.circles[5],this.circles[6]])
        this.circlesMap.set(this.circles[2], [this.circles[4],this.circles[6]])
        this.circlesMap.set(this.circles[3], [this.circles[5],this.circles[6]])
        this.circlesMap.set(this.circles[4], [this.circles[0],this.circles[2],this.circles[5],this.circles[6]])
        this.circlesMap.set(this.circles[5], [this.circles[1],this.circles[3],this.circles[4],this.circles[6]])
        this.circlesMap.set(this.circles[6], [this.circles[0],this.circles[1], this.circles[2], this.circles[3], this.circles[4], this.circles[5]])
        for (let i = 0; i < this.circles.length; i++){
            this.circles[i].index = i
            
        }
    }
    /** 生成连线 */
    generateLines() {
        for (const [circle1, circles] of this.circlesMap) {
            for (const circle2 of circles) {
                const p = circles.findIndex(i => i === circle1)
                const q = circles.findIndex(i => i === circle2)
                if (p < q) {
                    this.generateALine(circle1,circle2)
                }
            }
            
        }
    }

    generateALine(cur:CircleManager,next:CircleManager) {
        const line = instantiate(this.line)
        
        const { x: x1, y: y1 } = cur.node.position
        const { x: x2, y: y2 } = next.node.position
        const x = (x1 + x2) / 2
        const y = (y1 + y2) / 2
        line.setPosition(x, y)
        
        const side1 = Math.abs(x1 - x2)
        const side2 = Math.abs(y1 - y2)
        const side3 = Math.sqrt(side1 ** 2 + side2 ** 2)
        
        const UItransform = line.getComponent(UITransform)
        UItransform.setContentSize(side3 - 2 * 80, UItransform.contentSize.height)
        
        const rad = Math.atan(side2 / side1)
        const angle = rad * 180 / Math.PI
        const sign = (x1 < x2 && y1 < y2) || (x1 > x2 && y1 > y2) ? 1 : -1
        line.setRotationFromEuler(0, 0, sign * angle)
        
        this.lines.addChild(line)
    }
    
}


